package com.yssoft.service.purchase;

import com.google.common.collect.Lists;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.entity.core.TSupplier;
import com.yssoft.entity.purchase.*;
import com.yssoft.mapper.core.TDdDetailMapper;
import com.yssoft.mapper.core.TSupplierMapper;
import com.yssoft.mapper.purchase.*;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.UploadFile;
import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 陈翼东
 * @说明 采购管理
 * @time 2018/4/29 下午 03:14
 */
@Service
public class PurchaseInfoService {
    
    private static Logger logger = Logger.getLogger(PurchaseInfoService.class);
    
    @Resource
    private TPurchaseInfoMapper purchaseInfoMapper;
    @Resource
    private TPurchaseDetailMapper purchaseDetailMapper;
    @Resource
    private TDemandDetailMapper demandDetailMapper;
    @Resource
    private TDemandInfoMapper demandInfoMapper;
    @Resource
    private TSupplierMapper supplierMapper;
    @Resource
    private TRecipientDetailMapper recipientDetailMapper;
    @Resource
    private TPurchaseAttMapper purchaseAttMapper;
    @Resource
    private TRecipientAttMapper recipientAttMapper;
    @Resource
    private TDdDetailMapper ddDetailMapper;
    
    /**
     * 分页查询订单信息
     *
     * @param page
     * @param limit
     * @param companyId
     * @param goodsClassifyId
     * @param goodsName
     * @return
     */
    public List<Map<String, Object>> findPage(int page, int limit, int companyId, String goodsClassifyId, String goodsName) {
        Map<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        List<TDdDetail> detailList = null;
        if (goodsClassifyId != null && goodsClassifyId.length() > 0) {
            detailList = ddDetailMapper.queryChildrenDdDetail(Integer.parseInt(goodsClassifyId));
        } else {
            param.put("goodsClassifyId", null);
        }
        if (detailList != null && detailList.size() > 0) {
            param.put("goodsClassifyId", detailList);
        } else {
            param.put("goodsClassifyId", null);
        }
        param.put("goodsName", goodsName);
        param.put("page", page * limit);
        param.put("countNum", limit);
        List<Map<String, Object>> purchaseInfoList = purchaseInfoMapper.selectByConditions(param);
        for (Map<String, Object> purchaseInfo : purchaseInfoList) {
            Integer infoId = Integer.parseInt(purchaseInfo.get("info_id").toString());
            List<Map<String, Object>> detail = purchaseDetailMapper.selectByInfoId(infoId);
            purchaseInfo.put("purchaseRequestDetails", detail);
        }
        return purchaseInfoList;
    }
    
    
    /**
     * 修改采购申请单详情状态
     *
     * @param purchaseRequestId
     * @param stat              修改的状态
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object updateDemandInfoStat(String purchaseRequestId, String stat) {
        try {
            if (purchaseRequestId != null) {
                Integer infoId = Integer.parseInt(purchaseRequestId);
                TDemandInfo demandInfo = demandInfoMapper.selectByPrimaryKey(infoId);
                demandInfo.setStat(stat);
                demandInfoMapper.updateByPrimaryKeySelective(demandInfo);
                return AppUtil.returnMapJson("1", "保存成功", null);
            } else {
                return AppUtil.returnMapJson("0", "保存失败请传递采购单id", null);
            }
        } catch (NumberFormatException e) {
            return AppUtil.returnMapJson("0", "保存失败" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取我的供应商
     *
     * @param companyId
     * @param name
     * @return
     */
    public List<TSupplier> findMySupplier(String companyId, String name, int pageSize, int currentPage) {
        Map<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        param.put("companyName", name);
        param.put("page", currentPage * pageSize);
        param.put("countNum", pageSize);
        return supplierMapper.findMySupplier(param);
        
    }
    
    /**
     * 删除采购单(删除同时要将原来关联的申请订单详情状态变成待采购)
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object deleteOne(String id) {
        if (id == null) {
            return AppUtil.returnMapJson("0", "删除失败,请传递要删除的id", null);
        } else {
            List<Map<String, Object>> detailList = purchaseDetailMapper.selectByInfoId(Integer.parseInt(id));
            for (Map<String, Object> detailMap : detailList) {
                Object demandDetailId = detailMap.get("t_d_id");
                if (demandDetailId != null) {
                    TDemandDetail demandDetail = new TDemandDetail();
                    demandDetail.setId(Integer.parseInt(demandDetailId + ""));
                    demandDetail.setStat("1");//申请明细变成待采购
                    demandDetailMapper.updateByPrimaryKeySelective(demandDetail);
                    TDemandDetail newDemandDetail = demandDetailMapper.selectByPrimaryKey(Integer.parseInt(demandDetailId + ""));
                    //将该申请详情对应的申请单变成已申请
                    Integer demandInfoId = newDemandDetail.gettDId();//申请单
                    if (demandInfoId != null) {
                        TDemandInfo demandInfo = demandInfoMapper.selectByPrimaryKey(demandInfoId);
                        demandInfo.setStat("2");
                        demandInfoMapper.updateByPrimaryKeySelective(demandInfo);
                    }
                }
            }
            List<Map<String, Object>> purchaseDetailList = purchaseDetailMapper.selectByInfoId(Integer.parseInt(id));
            for (Map<String, Object> detail : purchaseDetailList) {
                Object detailId = detail.get("detail_id");
                TPurchaseDetail detail1 = new TPurchaseDetail();
                detail1.setId(Integer.parseInt(detailId.toString()));
                detail1.setDeleteFlag("1");
                purchaseDetailMapper.updateByPrimaryKeySelective(detail1);
            }
            TPurchaseInfo info = new TPurchaseInfo();
            info.setId(Integer.parseInt(id));
            info.setDeleteFlag("1");
            purchaseInfoMapper.updateByPrimaryKeySelective(info);
            return AppUtil.returnMapJson("1", "删除成功!", null);
        }
    }
    
    /**
     * 申请单保存为采购单
     *
     * @param entId
     */
    @Transactional(rollbackFor = Exception.class)
    public Object demandSavePurchase(String[] id, String[] singlePrice, String[] remark, String[] deliveryDate, String entId, HttpServletRequest request) {
        System.out.println(Arrays.toString(singlePrice));
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            
            TPurchaseInfo purchaseInfo = new TPurchaseInfo();
            String code = "C" + System.currentTimeMillis() + "";
            purchaseInfo.setCode(code);
            purchaseInfo.setStat("1");//已采购
            purchaseInfo.settEId(AppUserHelper.getCurrentCompanyId(request));
            if (entId != null) {
                purchaseInfo.settSId(Integer.parseInt(entId));
            } else {
                return AppUtil.returnMapJson("0", "保存失败，请传递供应商id", null);
            }
            purchaseInfo.setCreateTime(new Date());
            purchaseInfo.setCreatorId(AppUserHelper.getCurrentUserId(request));
            purchaseInfo.setCreatorName(AppUserHelper.getCurrentUserName(request));
            purchaseInfo.setDeleteFlag("0");
            purchaseInfoMapper.insert(purchaseInfo);
            BigDecimal zongjia = new BigDecimal(0);
            List<TPurchaseDetail> purchaseDetailList = new ArrayList<>();
            for (int j = 0; j < id.length; j++) {
                TDemandDetail demandDetail = demandDetailMapper.selectByPrimaryKey(Integer.parseInt(id[j]));
                TPurchaseDetail purchaseDetail = new TPurchaseDetail();
                purchaseDetail.setStat("1");//待收货状态
                purchaseDetail.setCode(code + j);
                purchaseDetail.setQuantity(demandDetail.getQuantity());
                purchaseDetail.setRemarks(remark.length >= id.length ? remark[j] : "");
                BigDecimal singlePriceBig = new BigDecimal(singlePrice.length >= id.length ? singlePrice[j] : "");
                purchaseDetail.setSinglePrice(singlePriceBig);
                zongjia.add(singlePriceBig.multiply(demandDetail.getQuantity()));//计算总价
                try {
                    if (deliveryDate.length >= id.length) {
                        purchaseDetail.setSupplyTime(simpleDateFormat.parse(deliveryDate[j]));
                    }
                } catch (ParseException e) {
                    logger.error("保存订单详情时间转换异常" + e.getMessage());
                    return AppUtil.returnMapJson("0", "保存失败，保存订单详情时间转换异常", null);
                }
                purchaseDetail.setDeleteFlag("0");
                purchaseDetail.setCreatorName(AppUserHelper.getCurrentUserName(request));
                purchaseDetail.setCreatorId(AppUserHelper.getCurrentUserId(request));
                purchaseDetail.setCreateTime(new Date());
                purchaseDetail.settDId(Integer.parseInt(id[j]));
                purchaseDetail.settPId(purchaseInfo.getId());
                purchaseDetail.settMId(demandDetail.gettMId());
                purchaseDetail.settEId(AppUserHelper.getCurrentCompanyId(request));
                purchaseDetailList.add(purchaseDetail);
                demandDetail.setStat("2");//申请明细变成已经采购
                demandDetailMapper.updateByPrimaryKeySelective(demandDetail);
                //判断申请详情对应的申请单是否全部已经下单，如果是将申请单状态改成4 已采购
                Integer demandId = demandDetail.gettDId();
                int count = demandInfoMapper.selectDetailStatByInfoId(demandId);
                if (count <= 0) {
                    TDemandInfo demandInfo = demandInfoMapper.selectByPrimaryKey(demandId);
                    demandInfo.setStat("4");
                    demandInfoMapper.updateByPrimaryKeySelective(demandInfo);
                }
            }
            purchaseDetailMapper.insertAll(purchaseDetailList);
            purchaseInfo.setTotalPrice(zongjia);
            purchaseInfoMapper.updateByPrimaryKeySelective(purchaseInfo);
            return AppUtil.returnMapJson("1", "保存成功", null);
        } catch (NumberFormatException e) {
            return AppUtil.returnMapJson("0", "保存失败" + e.getMessage(), null);
        }
    }
    
    
    /**
     * 保存采购单
     *
     * @param realPrice
     * @param reciveDate
     * @param remark
     * @param mid
     * @param quantity
     * @param totalPrice
     * @param supplierId
     */
    @Transactional(rollbackFor = Exception.class)
    public Object savePurchaseInfo(String[] realPrice, String[] reciveDate, String[] remark, String[] mid, String[] quantity, String totalPrice, String supplierId, HttpServletRequest request) throws IOException, DecoderException {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            TPurchaseInfo purchaseInfo = new TPurchaseInfo();
            String code = "C" + System.currentTimeMillis() + "";
            purchaseInfo.setCode(code);
            purchaseInfo.setStat("1");//已采购
            purchaseInfo.settEId(AppUserHelper.getCurrentCompanyId(request));
            if (supplierId != null) {
                purchaseInfo.settSId(Integer.parseInt(supplierId));
            } else {
                return AppUtil.returnMapJson("0", "保存失败，请传递供应商id", null);
            }
            
            purchaseInfo.setCreateTime(new Date());
            purchaseInfo.setCreatorId(AppUserHelper.getCurrentUserId(request));
            purchaseInfo.setCreatorName(AppUserHelper.getCurrentUserName(request));
            purchaseInfo.setDeleteFlag("0");
            BigDecimal zongjia = new BigDecimal(totalPrice);
            purchaseInfo.setTotalPrice(zongjia);
            
            purchaseInfoMapper.insert(purchaseInfo);
            List<TPurchaseDetail> purchaseDetailList = new ArrayList<>();
            //保存详情
            
            for (int i = 0; i < realPrice.length; i++) {
                TPurchaseDetail purchaseDetail = new TPurchaseDetail();
                purchaseDetail.setStat("1");//待收货状态
                purchaseDetail.setCode(code + i);
                purchaseDetail.setQuantity(new BigDecimal(quantity.length >= realPrice.length ? quantity[i] : ""));
                purchaseDetail.setRemarks(remark.length >= realPrice.length ? remark[i] : "");
                BigDecimal singlePrice = new BigDecimal(realPrice[i]);
                purchaseDetail.setSinglePrice(singlePrice);
                try {
                    if (reciveDate.length >= realPrice.length)
                        purchaseDetail.setSupplyTime(simpleDateFormat.parse(reciveDate[i]));
                } catch (ParseException e) {
                    logger.error("保存订单详情时间转换异常" + e.getMessage());
                    return AppUtil.returnMapJson("0", "保存失败，保存订单详情时间转换异常", null);
                }
                purchaseDetail.setDeleteFlag("0");
                purchaseDetail.setCreatorName(AppUserHelper.getCurrentUserName(request));
                purchaseDetail.setCreatorId(AppUserHelper.getCurrentUserId(request));
                purchaseDetail.setCreateTime(new Date());
                purchaseDetail.settPId(purchaseInfo.getId());
                purchaseDetail.settMId(Integer.parseInt(mid.length >= realPrice.length ? mid[i] : ""));
                purchaseDetail.settEId(AppUserHelper.getCurrentCompanyId(request));
                purchaseDetailList.add(purchaseDetail);
            }
            purchaseDetailMapper.insertAll(purchaseDetailList);
    
            MultipartRequest multipartRequest=(MultipartRequest)request;
            Iterator<String> fileNames = multipartRequest.getFileNames();
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            List<MultipartFile> files = Lists.newArrayList();
            while (fileNames.hasNext()){
                files.add(fileMap.get(fileNames.next()));
            }
            if (files != null && files.size() > 0) {
                for (MultipartFile multipartFile : files) {
                    Map<String, Object> urlMap = UploadFile.compressUploadedFile(multipartFile);
                    TPurchaseAtt purchaseAtt = new TPurchaseAtt();
                    purchaseAtt.settEId(AppUserHelper.getCurrentCompanyId(request));
                    purchaseAtt.settPId(purchaseInfo.getId());
                    purchaseAtt.setType("1");
                    purchaseAtt.setCreateTime(new Date());
                    purchaseAtt.setCreatorId(AppUserHelper.getCurrentUserId(request));
                    purchaseAtt.setCreatorName(AppUserHelper.getCurrentUserName(request));
                    purchaseAtt.setDeleteFlag("0");
                    purchaseAtt.setAttTimg(urlMap.get("thumbnails")+"");
                    purchaseAtt.setAttImg(urlMap.get("fileName")+"");
                    purchaseAttMapper.insertSelective(purchaseAtt);
                }
            }
            return AppUtil.returnMapJson("1", "保存成功!", null);
        } catch (Exception e) {
            return AppUtil.returnMapJson("0", "保存失败" + e.getMessage(), null);
        }
    }
    
    /**
     * 根据订单详情id查询收货单记录
     *
     * @param purchaseOrderDetailId
     * @return
     */
    public Map<String, Object> findRecipientByPurchaseDetailId(String purchaseOrderDetailId) throws IllegalAccessException {
        Map<String, Object> detail = purchaseDetailMapper.selectDetailByPrimaryKey(Integer.parseInt(purchaseOrderDetailId));
        List<Map<String, Object>> reciveDocumentList = recipientDetailMapper.selectByPurchaseDetailId(Integer.parseInt(purchaseOrderDetailId));
        for (Map<String, Object> recivetMap : reciveDocumentList) {
            List<Map<String, Object>> att = recipientAttMapper.selectByReciveId(Integer.parseInt(recivetMap.get("id") + ""));
            recivetMap.put("att", att);
        }
        detail.put("reciveDocumentList", reciveDocumentList);
        return detail;
    }
    
    /**
     * 上传订单票据
     *
     * @param files
     * @param purchaseInfoId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Object uploadContract(List<MultipartFile> files, String purchaseInfoId, HttpServletRequest request) throws IOException, DecoderException {
        for (MultipartFile file : files) {
            Map<String, Object> urlMap = null;
            try {
                urlMap = UploadFile.compressUploadedFile(file);
            } catch (Exception e) {
                return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
            }
            
            if (purchaseInfoId == null) {
                return AppUtil.returnMapJson("0", "请传递订单的id", null);
            }
            
            
            TPurchaseAtt att = new TPurchaseAtt();
            att.setAttTimg(urlMap.get("thumbnails") + "");
            att.setAttImg(urlMap.get("fileName") + "");
            att.setType("1");
            
            att.settEId(AppUserHelper.getCurrentCompanyId(request));
            att.setDeleteFlag("0");
            att.setCreateTime(new Date());
            att.setCreatorName(AppUserHelper.getCurrentUserName(request));
            att.setCreatorId(AppUserHelper.getCurrentUserId(request));
            att.settPId(Integer.parseInt(purchaseInfoId));
            purchaseAttMapper.insertSelective(att);
            
        }
        return AppUtil.returnMapJson("1", "保存成功!", null);
        
    }
    
    /**
     * 查询一个订单
     *
     * @param purchaseInfoId
     * @return
     */
    public TPurchaseInfo findOne(String purchaseInfoId) {
        return purchaseInfoMapper.selectByPrimaryKey(Integer.parseInt(purchaseInfoId));
    }
    
    /**
     * 根据订单查询详情
     *
     * @param id
     * @return
     */
    public List<Map<String, Object>> findDetailByInfo(Integer id) {
        return purchaseDetailMapper.selectByInfoId(id);
    }
    
    /**
     * 修改补录的订单(删除原来的详情，新加详情)只有补录的才有修改
     *
     * @param realPrice
     * @param reciveDate
     * @param remark
     * @param mid
     * @param quantity
     * @param totalPrice
     * @param supplierId
     * @param request
     * @param purchaseInfoId
     * @return
     */
    public Object updatePurchaseInfo(String[] realPrice, String[] reciveDate, String[] remark, String[] mid, String[] quantity, String totalPrice, String supplierId, HttpServletRequest request, String purchaseInfoId) {
        if (purchaseInfoId == null) {
            return AppUtil.returnMapJson("0", "请传递订单id", null);
        } else {
            TPurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(Integer.parseInt(purchaseInfoId));
            //将原来的申请单便会
            List<Map<String, Object>> detailList = purchaseDetailMapper.selectByInfoId(Integer.parseInt(purchaseInfoId));
            for (Map<String, Object> detailMap : detailList) {
                Object demandDetailId = detailMap.get("t_d_id");
                if (demandDetailId != null) {
                    TDemandDetail demandDetail = new TDemandDetail();
                    demandDetail.setId(Integer.parseInt(demandDetailId + ""));
                    demandDetail.setStat("1");//申请明细变成待采购
                    demandDetailMapper.updateByPrimaryKeySelective(demandDetail);
                    TDemandDetail newDemandDetail = demandDetailMapper.selectByPrimaryKey(Integer.parseInt(demandDetailId + ""));
                    //将该申请详情对应的申请单变成待申请
                    Integer demandInfoId = newDemandDetail.gettDId();//申请单
                    if (demandInfoId != null) {
                        TDemandInfo demandInfo = demandInfoMapper.selectByPrimaryKey(demandInfoId);
                        demandInfo.setStat("1");
                        demandInfoMapper.updateByPrimaryKeySelective(demandInfo);
                    }
                }
            }
            List<Map<String, Object>> oldPurchaseDetailList = purchaseDetailMapper.selectByInfoId(Integer.parseInt(purchaseInfoId));
            for (Map<String, Object> detailMap : oldPurchaseDetailList) {
                TPurchaseDetail purchaseDetail = new TPurchaseDetail();
                purchaseDetail.setId(Integer.parseInt(detailMap.get("detail_id").toString()));
                purchaseDetail.setDeleteFlag("1");
                purchaseDetailMapper.updateByPrimaryKeySelective(purchaseDetail);
            }
            purchaseInfo.setTotalPrice(new BigDecimal(totalPrice));
            purchaseInfo.settSId(Integer.parseInt(supplierId));
            purchaseInfoMapper.updateByPrimaryKeySelective(purchaseInfo);
            //  try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<TPurchaseDetail> purchaseDetailList = new ArrayList<>();
            //保存详情
            for (int i = 0; i < realPrice.length; i++) {
                TPurchaseDetail purchaseDetail = new TPurchaseDetail();
                purchaseDetail.setStat("1");//待收货状态
                purchaseDetail.setCode(purchaseInfo.getCode() + i);
                
                purchaseDetail.setQuantity(new BigDecimal(quantity.length >= realPrice.length ? quantity[i] : ""));
                purchaseDetail.setRemarks(remark.length >= realPrice.length ? remark[i] : "");
                BigDecimal singlePrice = new BigDecimal(realPrice[i]);
                purchaseDetail.setSinglePrice(singlePrice);
                try {
                    if (reciveDate.length >= realPrice.length)
                        purchaseDetail.setSupplyTime(simpleDateFormat.parse(reciveDate[i]));
                } catch (ParseException e) {
                    logger.error("保存订单详情时间转换异常" + e.getMessage());
                    return AppUtil.returnMapJson("0", "保存失败，保存订单详情时间转换异常", null);
                }
                
                purchaseDetail.setDeleteFlag("0");
                purchaseDetail.setCreatorName(AppUserHelper.getCurrentUserName(request));
                purchaseDetail.setCreatorId(AppUserHelper.getCurrentUserId(request));
                purchaseDetail.setCreateTime(new Date());
                purchaseDetail.settPId(purchaseInfo.getId());
                purchaseDetail.settMId(Integer.parseInt(mid.length >= realPrice.length ? mid[i] : ""));
                purchaseDetail.settEId(AppUserHelper.getCurrentCompanyId(request));
                purchaseDetailList.add(purchaseDetail);
            }
            purchaseDetailMapper.insertAll(purchaseDetailList);
            //上传采购单图片的
    
            MultipartRequest multipartRequest=(MultipartRequest)request;
            Iterator<String> fileNames = multipartRequest.getFileNames();
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            List<MultipartFile> files = Lists.newArrayList();
            while (fileNames.hasNext()){
                files.add(fileMap.get(fileNames.next()));
            }
                if(files!=null && files.size()>0) {
                    for (MultipartFile multipartFile : files) {
                        Map<String, Object> urlMap = UploadFile.compressUploadedFile(multipartFile);
                        TPurchaseAtt purchaseAtt = new TPurchaseAtt();
                        purchaseAtt.settEId(AppUserHelper.getCurrentCompanyId(request));
                        purchaseAtt.settPId(purchaseInfo.getId());
                        purchaseAtt.setType("1");
                        purchaseAtt.setCreateTime(new Date());
                        purchaseAtt.setCreatorId(AppUserHelper.getCurrentUserId(request));
                        purchaseAtt.setCreatorName(AppUserHelper.getCurrentUserName(request));
                        purchaseAtt.setDeleteFlag("0");
                        purchaseAtt.setAttTimg(urlMap.get("thumbnails")+"");
                        purchaseAtt.setAttImg(urlMap.get("fileName")+"");
                        purchaseAttMapper.insertSelective(purchaseAtt);
                    }
                }
            return AppUtil.returnMapJson("1", "保存成功!", null);
            //} catch (Exception e) {
            //       return AppUtil.returnMapJson("0", "保存失败" + e.getMessage(), null);
            //  }
            
        }
        
    }
    
    public Object getContract(String purchaseInfoId) {
        try {
            return AppUtil.returnListJson("1", "获取成功!", purchaseAttMapper.selectByPurchaseInfoId(Integer.parseInt(purchaseInfoId)));
        } catch (NumberFormatException e) {
            return AppUtil.returnMapJson("0", "获取失败!" + e.getMessage(), null);
        }
    }
    
    /**
     * 获取申请单信息
     *
     * @param ids （ids=1,2,3,4,5）
     * @return
     */
    public Object findApplicationFormByid(String ids) {
        if (ids != null && ids.length() > 0) {
            String[] idArray = ids.split(",");
            List<Map<String, Object>> applicationForm = demandDetailMapper.selectByInfoIds(idArray);
            return AppUtil.returnListJson("1", "查询成功!", applicationForm);
        } else {
            return AppUtil.returnMapJson("0", "查询失败", null);
        }
        
        
    }
    
    /**
     * 根据订单查询订单票据照片
     * @param id
     * @return
     */
    public List<Map<String, Object>> findPurchaseImg(Integer id) {
        return purchaseAttMapper.selectByPurchaseInfoId(id);
    }
}
